package com.ruoyi.pingjiao.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.pingjiao.domain.*;
import com.ruoyi.pingjiao.domain.query.AddEvaluationQueryParam;
import com.ruoyi.pingjiao.domain.query.StatisticQueryParam;
import com.ruoyi.pingjiao.domain.vo.EvaluationReportVO;
import com.ruoyi.pingjiao.domain.vo.PaperItemVo;
import com.ruoyi.pingjiao.mapper.EvaluationItemMapper;
import com.ruoyi.pingjiao.mapper.EvalutionActivityMapper;
import com.ruoyi.pingjiao.mapper.StudentMapper;
import com.ruoyi.pingjiao.service.ICourseclassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pingjiao.mapper.EvaluationMapper;
import com.ruoyi.pingjiao.service.IEvaluationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评教管理Service业务层处理
 *
 * @author 胡浩鹏
 * @date 2022-10-22
 */

@Service
public class EvaluationServiceImpl implements IEvaluationService
{
    @Autowired
    private EvaluationMapper evaluationMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private EvalutionActivityMapper activityMapper;
    @Autowired
    private EvaluationItemMapper evaluationItemMapper;
    @Autowired
    private ICourseclassService courseclassService;

    /**
     * 查询评教管理
     *
     * @param evaluationId 评教管理主键
     * @return 评教管理
     */
    @Override
    public Evaluation selectEvaluationByEvaluationId(Long evaluationId)
    {
        return evaluationMapper.selectEvaluationByEvaluationId(evaluationId);
    }

    /**
     * 查询评教管理列表
     *
     * @param evaluation 评教管理
     * @return 评教管理
     */
    @Override
    public List<Evaluation> selectEvaluationList(Evaluation evaluation)
    {
        List<Evaluation> evaluations = evaluationMapper.selectEvaluationList(evaluation);

        return evaluations;
    }

    @Override
    public int statsGetComp(Long evalutionActivityId) {
        return evaluationMapper.statsGetComp(evalutionActivityId);
    }

    @Override
    public int statsGetShould(String courseType) {
        return evaluationMapper.statsGetShould(courseType);
    }

    @Override
    public void checkActivityStatus(){
        EvalutionActivity ea = activityMapper.selectEvalutionActivityByEvalutionActivityId((long) 1);
        int getShould = statsGetShould(ea.getCourseType());
        int getComp = statsGetComp(ea.getEvalutionActivityId());
        System.out.println("getShould :"+ getShould );
        System.out.println("getComp :" + getComp);
        if ( getComp < getShould){
            return ;
        }
        System.out.println("88888888888888");
        EvalutionActivity activity = new EvalutionActivity();
        activity.setEvalutionActivityId(ea.getEvalutionActivityId());
        activity.setStatus("4");
        activityMapper.updateEvalutionActivity(activity);
    }

    /**
     * 新增评教管理
     *
     * @param evaluation 评教管理
     * @return 结果
     */
    @Override
    public int insertEvaluation(Evaluation evaluation)
    {
        evaluation.setCreateTime(DateUtils.getNowDate());
        return evaluationMapper.insertEvaluation(evaluation);
    }

    @Transactional
    @Override
    public int insertEvaluations(Teacher teacher) {

        Evaluation evaluation = new Evaluation();
        List<Evaluation> evaluationList =new ArrayList<>();
        EvaluationItem e = new EvaluationItem();
        List<EvaluationItem> evaluationItemList = new ArrayList<>();
        int res = 0;

        //s从登录用户信息中取得
        Student s = studentMapper.selectStudentByStudentId((long) 1);
        //从缓存中获得
        EvalutionActivity evalutionActivity = activityMapper.selectEvalutionActivityByEvalutionActivityId((long) 1);

        for (Courseclass cc : teacher.getCourseclassList()){

            System.out.println(cc);

            float sum = 0;

            for (PaperItemCategory category : cc.getCourse().getPaper().getPaperItemCategoryList()){
                for (PaperItem item : category.getPaperItemList()){
                    EvaluationItem evaluationItem = new EvaluationItem();
                    //BeanUtils.copyProperties(e,evaluation);
                    float score = Float.valueOf(item.getItemPointValue());
                    sum = sum + score;
                    //evaluationItem.setEvaluationId(evaluation.getEvaluationId());
                    evaluationItem.setPaperItemId(item.getPaperItemId());
                    evaluationItem.setScore(score);
                    //evaluationItemMapper.insertEvaluationItem(evaluationItem);
                    evaluationItemList.add(evaluationItem);
                }

            }
            System.out.println("===="+sum);
            //组织evaluation
            evaluation.setTeacherId(teacher.getTeacherId());
            evaluation.setStudentId(s.getStudentId());
            evaluation.setCourseclassId(cc.getCourseclassId());
            evaluation.setEvaluationPaperId(cc.getCourse().getPaper().getEvaluationPaperId());
            evaluation.setEvaluationActivityId(evalutionActivity.getEvalutionActivityId());
            evaluation.setSumScore(String.valueOf(sum));

            System.out.println(evaluation);

            //插入
            int i = evaluationMapper.insertEvaluation(evaluation);

            for (EvaluationItem item : evaluationItemList){
                item.setEvaluationId(evaluation.getEvaluationId());
                System.out.println(item);
                evaluationItemMapper.insertEvaluationItem(item);
            }

            res = res + 1;
        }


        return res;
    }
//    abandoned
//    @Transactional
//    @Override
//    public int insertEvaluationByQuerys(AddEvaluationQueryParam queryParam) {
//
//        Evaluation evaluation = new Evaluation();
//        List<EvaluationItem> evaluationItemList = new ArrayList<>();
//        double sum = 0;
//        for (PaperItemVo vo : queryParam.getPaperItemList()){
//            EvaluationItem e = new EvaluationItem();
//            e.setPaperItemId(vo.getPaperItemId());
//            e.setSelectedOption(vo.getOption());
//            double score = 0;
//            switch (vo.getOption()) {
//                case "A":
//                    score = Long.valueOf(vo.getItemPointValue()) * 1;
//                    break;
//                case "B":
//                    score = Long.valueOf(vo.getItemPointValue()) * 0.9;
//                    break;
//                case "C":
//                    score = Long.valueOf(vo.getItemPointValue()) * 0.8;
//                    break;
//                case "D":
//                    score = Long.valueOf(vo.getItemPointValue()) * 0.7;
//                    break;
//                case "E":
//                    score = Long.valueOf(vo.getItemPointValue()) * 0.6;
//                    break;
//                default:
//                    score = 0;
//                    break;
//            }
//            sum = sum + score;
//            e.setScore((float) (Math.round(score*10))/10);//保留一位小数
//            evaluationItemList.add(e);
//        }
//
//        evaluation.setTeacherId(queryParam.getTeacherId());
//        evaluation.setStudentId(queryParam.getStudentId());
//        evaluation.setEvaluationActivityId(queryParam.getEvaluationActivityId());
//        evaluation.setCourseclassId(queryParam.getCourseclassId());
//        evaluation.setEvaluationPaperId(queryParam.getEvaluationPaperId());
//        evaluation.setSumScore(String.format("%.2f",sum));
//
//        int i = evaluationMapper.insertEvaluation(evaluation);
//        for (EvaluationItem item : evaluationItemList){
//            item.setEvaluationId(evaluation.getEvaluationId());
//            evaluationItemMapper.insertEvaluationItem(item);
//        }
//        return i;
//    }

    @Transactional
    @Override
    public int updateEvaluation(AddEvaluationQueryParam queryParam) {

        Evaluation evaluation =selectEvaluationByEvaluationId(queryParam.getEvaluationId());
        List<EvaluationItem> evaluationItemList = new ArrayList<>();
        double sum = 0;
        for (PaperItemVo vo : queryParam.getPaperItemList()){
            EvaluationItem e = new EvaluationItem();
            e.setPaperItemId(vo.getPaperItemId());
            e.setSelectedOption(vo.getOption());
            double score = 0;
            switch (vo.getOption()) {
                case "A":
                    score = Long.valueOf(vo.getItemPointValue()) * 1;
                    break;
                case "B":
                    score = Long.valueOf(vo.getItemPointValue()) * 0.9;
                    break;
                case "C":
                    score = Long.valueOf(vo.getItemPointValue()) * 0.8;
                    break;
                case "D":
                    score = Long.valueOf(vo.getItemPointValue()) * 0.7;
                    break;
                case "E":
                    score = Long.valueOf(vo.getItemPointValue()) * 0.6;
                    break;
                default:
                    score = 0;
                    break;
            }
            sum = sum + score;
            e.setScore((float) (Math.round(score*10))/10);//保留一位小数
            evaluationItemList.add(e);
        }
        evaluation.setSumScore(String.format("%.2f",sum));
        int i= updateEvaluation(evaluation);
        for (EvaluationItem item : evaluationItemList){
            item.setEvaluationId(evaluation.getEvaluationId());
            evaluationItemMapper.insertEvaluationItem(item);
        }
        return  i;
    }
    /**
     * 修改评教管理
     *
     * @param evaluation 评教管理
     * @return 结果
     */
    @Override
    public int updateEvaluation(Evaluation evaluation)
    {
        evaluation.setUpdateTime(DateUtils.getNowDate());
        return evaluationMapper.updateEvaluation(evaluation);
    }

    /**
     * 批量删除评教管理
     *
     * @param evaluationIds 需要删除的评教管理主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationByEvaluationIds(Long[] evaluationIds)
    {
        return evaluationMapper.deleteEvaluationByEvaluationIds(evaluationIds);
    }

    /**
     * 删除评教管理信息
     *
     * @param evaluationId 评教管理主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationByEvaluationId(Long evaluationId)
    {
        return evaluationMapper.deleteEvaluationByEvaluationId(evaluationId);
    }
    /**
     * 判断学生是否评价老师
     * @param map
     * @return
     */
    public float getTeacherEvaluatedScore(Map<String,Object> map){
        Float result=evaluationMapper.getTeacherEvaluatedScore(map);
        if(result!=null){
            return result;
        }else{
            return 0;
        }
    }
    /**
     * 保存评价的教师最终成绩，并保存在历史记录表中
     * @return
     */
    @Override
    @Transactional
    public  int saveEvalScoreOfTeacher(Long evaluationActivityId){
        evaluationMapper.deleteHistory(evaluationActivityId);//防止重复统计时产生重复的记录，所以先清除。
        //原逻辑
//        evaluationMapper.statEvaluatedScoreOfTeacher(evaluationActivityId);
//        evaluationMapper.addEvaluationHistory(evaluationActivityId);
        evaluationMapper.statEvaluatedScoreOfTeacher(evaluationActivityId);
        evaluationMapper.statEvaluatedScoreAndVotingRateOfTeacher(evaluationActivityId);
        evaluationMapper.addEvaluationHistoryNew(evaluationActivityId);
        return 1;
    }
    @Override
    public int add(Long evaluationActivityId,Long evaluationPaperId,String courseType, String term){
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("evaluationActivityId",evaluationActivityId);
        map.put("evaluationPaperId",evaluationPaperId);
        map.put("courseType",courseType);
        map.put("term",term);
        return  evaluationMapper.add(map);
    }

    @Override
    public List<EvaluationReportVO> getAllEvaluationReport(StatisticQueryParam queryParam){
        return  evaluationMapper.getAllEvaluationReportNew(queryParam);
    }


    @Override
    public List<Map<String, Object>> getTeacherEvaluationPaperItemScoreReport(Long evaluationActivityId,Long teacherId) {
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("evaluation_activity_id",evaluationActivityId);
        map.put("teacher_id",teacherId);
        return evaluationMapper.getTeacherEvaluationPaperItemScoreReport(map);
    }
    @Override
    public List<EvaluationReportVO> getDeptScopeTeacherEvaluationReport(Long evaluationActivityId,Long deptId){
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("evaluation_activity_id",evaluationActivityId);
        map.put("dept_id",deptId);
        return  evaluationMapper.getDeptScopeTeacherEvaluationReport(map);
    }
    @Override
    public List<Map<String, Object>> getTeacherCourseClassEvaluationItemScoreReport(Map<String, Object> map) {
        return evaluationMapper.getTeacherCourseClassEvaluationItemScoreReport(map);
    }

    @Override
    public List<Map<String, Object>> getTeacherCourseEvaluationItemScoreReport(Map<String, Object> map) {
        return evaluationMapper.getTeacherCourseEvaluationItemScoreReport(map);
    }


    /**
     * cjb,统计全校教师评教各分数段人数
     */
    public List<Map<String,Object>> getGradeWithActivityId(Long evaluation_actitityId){
        return evaluationMapper.getGradeWithActivityId(evaluation_actitityId);
    }

    /**
     * cjb,按院系统计教师评教各分数段人数
     */
    public List<Map<String,Object>> getGradeWithActivityIdAndDeptName(Map<String,Object> map){
        return  evaluationMapper.getGradeWithActivityIdAndDeptName(map);
    }

    @Override
    public List<Map<String, Object>> getDeptRank(Long evaluationActivityId) {
       return  evaluationMapper.getDeptRank(evaluationActivityId);
    }

    @Override
    public List<Map<String, Object>> countEvaluationProgressByEvaluationActivityIdAndTeacherId(Long evaluation_activityId, Long teacherId) {
        return evaluationMapper.countEvaluationProgressByEvaluationActivityIdAndTeacherId(evaluation_activityId,teacherId);
    }

    @Override
    public List<Map<String, Object>> countEvaluationProgress(Long evalutionActivityId,Long teacherId) {
        //获取当前评教活动
//        ToUpdate
        List<Map<String, Object>> maps =
                countEvaluationProgressByEvaluationActivityIdAndTeacherId(evalutionActivityId, teacherId);
        return maps;
    }

    @Override
    public List<Map<String, Object>> countEvaluationProgressFromCourseclass(Long teacherId) {
        List<Map<String, Object>> maps =
                evaluationMapper.countEvaluationProgressFromCourseclass(teacherId);
        return maps;
    }

    @Override
    public int deleteCourseclassHistory(Long evaluationActivityId) {
        return evaluationMapper.deleteCourseclassHistory(evaluationActivityId);
    }

    @Override
    public Object getEvaluationIdByEvaluationPaperId(Long evaluationPaperId) {
        return evaluationMapper.getEvaluationIdByEvaluationPaperId(evaluationPaperId);
    }


}
